<template>
  <section>
    <!--抽屉-->
    <el-button type="text" @click="table = true">打开嵌套表格的 Drawer</el-button>
      <el-drawer
          title="产品详细信息"
          :visible.sync="table"
          direction="rtl"
          size="50%">
        <!--查询、新增  工具条-->
        <el-col :span="24" class="toolbar" style="padding-bottom: 0px;">
          <el-form :inline="true" :model="filters">
            <el-form-item>
              <el-input v-model="filters.keyword" placeholder="姓名" clearable></el-input>
            </el-form-item>
            <el-form-item>
              <el-button type="primary" v-on:click="getDetailsTwo" clearable>查询</el-button>
            </el-form-item>
            <el-form-item>
              <el-button type="primary" @click="handleAddTwo()">新增</el-button>
            </el-form-item>
          </el-form>
        </el-col>
        <!--列表-->
        <el-table :data="detail">
          <el-table-column property="id" label="产品类型" width="300"></el-table-column>
          <el-table-column property="name" label="产品单位" width="300"></el-table-column>

            <!--<template slot-scope="scope">
              {{getChangeType(scope.row.types_id)}}
            </template>-->
          <el-table-column label="操作" width="120px">
            <template scope="scope">
              <el-button size="small" @click="handlesaveTwo(scope.$index, scope.row)">编辑</el-button>
              <el-button type="danger" size="small" @click="handleDelTwo(scope.$index, scope.row)">删除</el-button>
            </template>
          </el-table-column>
        </el-table>
        <!--分页工具条-->
        <el-col :span="24" class="toolbar">
          <el-pagination
              @size-change="handleSizeChangeTwo"
              @current-change="handleCurrentChangeTwo"
              :current-page="currentPages"
              :page-sizes="[5, 10, 15, 30]"
              :page-size="pageSizes"
              layout="total, sizes, prev, pager, next, jumper"
              :total="totals"
              style="float:right;">
          </el-pagination>
        </el-col>
        <!--编辑界面-->
        <el-dialog :title="saveTitle" :visible.sync="saveFormVisible" :close-on-click-modal="false" :append-to-body="true">
          <el-form :model="saveFormTwo" label-width="120px" :rules="saveFormRules" ref="saveFormTwo">
            <el-form-item label="产品单位" prop="name">
              <el-input v-model="saveFormTwo.name" auto-complete="off"></el-input>
            </el-form-item>
          </el-form>
          <div slot="footer" class="dialog-footer">
            <el-button @click.native="saveFormVisible = false">取消</el-button>
            <el-button type="primary" @click.native="saveSubmitTwo" :loading="saveLoading">提交</el-button>
          </div>
        </el-dialog>
      </el-drawer>



    <!--查询、新增  工具条-->
    <el-col :span="24" class="toolbar" style="padding-bottom: 0px;">
      <el-form :inline="true" :model="filters">
        <el-form-item>
          <el-input v-model="filters.keyword" placeholder="姓名" clearable></el-input>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" v-on:click="getType" clearable>查询</el-button>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="handleAdd()">新增</el-button>
        </el-form-item>
      </el-form>
    </el-col>
    <!--列表-->
    <el-table :data="types" highlight-current-row v-loading="listLoading" @selection-change="selsChange" style="width: 100%;"@row-dblclick="getChouti">
      <el-table-column type="selection" width="55">
      </el-table-column>
      <el-table-column type="index" width="60">
      </el-table-column>
      <el-table-column prop="name" label="产品类型" width="400" sortable>
      </el-table-column>
      <el-table-column prop="sn" label="产品名称" width="400" sortable>
      </el-table-column>
      <el-table-column label="操作" width="150">
        <template scope="scope">
          <el-button size="small" @click="handlesave(scope.$index, scope.row)">编辑</el-button>
          <el-button type="danger" size="small" @click="handleDel(scope.$index, scope.row)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <!--分页、批量删除 工具条-->
    <el-col :span="24" class="toolbar">
      <el-button type="danger" @click="batchRemove" :disabled="this.sels.length===0">批量删除</el-button>
      <el-pagination
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
          :current-page="currentPage"
          :page-sizes="[5, 10, 15, 30]"
          :page-size="pageSize"
          layout="total, sizes, prev, pager, next, jumper"
          :total="total"
          style="float:right;">
      </el-pagination>
    </el-col>
    <!--编辑界面-->
    <el-dialog :title="saveTitle" :visible.sync="saveFormVisible" :close-on-click-modal="false" :append-to-body="true">
      <el-form :model="saveForm" label-width="120px" :rules="saveFormRules" ref="saveFormTwo">
        <el-form-item label="产品单位" prop="sn">
          <el-input v-model="saveForm.sn" auto-complete="off"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click.native="saveFormVisible = false">取消</el-button>
        <el-button type="primary" @click.native="saveSubmit" :loading="saveLoading">提交</el-button>
      </div>
    </el-dialog>
    <!--<el-dialog :title="saveTitle" :visible.sync="saveFormVisible" :close-on-click-modal="false">
      <el-form :model="saveForm" label-width="80px"  ref="saveForm">
        <el-form-item label="产品类型" prop="name">
          <el-input v-model="saveForm.name" auto-complete="off"></el-input>
        </el-form-item>
        <el-form-item label="产品类型" prop="sn">
          <el-input v-model="saveForm.sn" auto-complete="off"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click.native="saveFormVisible = false">取消</el-button>
        <el-button type="primary" @click.native="saveSubmit" :loading="saveLoading">提交</el-button>
      </div>
    </el-dialog>-->

  </section>
</template>

<script>

export default {
  data() {
    return {
      filters: {
        keyword: ''
      },
      types_id:null,
      //数据字典
      types: [],
      detail: [],

      table: false,
      total: 0,
      currentPage: 1,
      pageSize: 10,
      //抽屉
      totals: 0,
      currentPages: 1,
      pageSizes: 10,

      listLoading: false,
      sels: [],//列表选中列

      saveTitle:'',
      saveFormVisible: false,//编辑界面是否显示
      saveLoading: false,//保存忙等
      saveFormRules: {
        name: [
          { required: true, message: '请输入姓名', trigger: 'blur' }
        ]
      },
      //编辑界面数据(字典)
      saveFormTwo: {
        id: null,
        name: null,
        types_id:null,
      },
      saveForm: {//编辑界面数据
        id: null,
        name: '',
        sn: ''
      },


      addFormVisible: false,//新增界面是否显示
      addLoading: false,//添加忙等
      addFormRules: {
        name: [
          { required: true, message: '请输入姓名', trigger: 'blur' }
        ]
      },
    }
  },
  methods: {
    handleSizeChangeTwo(val){
      this.pageSizes = val
      this.getDetailsTwo();
    },
    handleCurrentChangeTwo(val) {
      this.currentPages = val;
      this.getDetailsTwo();
    },
    handleSizeChange(val){
      this.pageSize = val
      this.getType();

    },
    handleCurrentChange(val) {
      this.currentPage = val;
      this.getType();
    },

    //获取系统字典详细信息(抽屉)
    getdetail(){
      this.$http.get("/sdd/list").then(res=>{
        this.detail = res.data
      })
    },
    //删除(抽屉)
    handleDelTwo: function (index, row) {
      this.$confirm('确认删除该记录吗?', '提示', {
        type: 'warning'
      }).then(() => {
        this.listLoading = true;
        this.$http.delete("/sdd/"+row.id).then(res=>{
          if(res.data.success){
            this.listLoading = false;
            //NProgress.done();
            this.$message({
              message: res.data.msg,
              type: 'success'
            });
            this.getDetailsTwo();
          }else{
            this.$message({
              message: res.data.msg,
              type: 'warning'
            });
          }
        })
      }).catch(() => {

      });
    },
    //获取用户列表(抽屉)
    getDetailsTwo() {
      let para = {
        currentPages: this.currentPages,
        pageSizes: this.pageSizes,
        //-------------------------关键字查询-------------------------------------
        keyword: this.filters.keyword,
        types_id:this.types_id
      };
      this.listLoading = true;
      //NProgress.start();
      this.$http.post("/sdd/queryList",para).then(res=>{
        console.log(res)
        this.detail = res.data.date
        this.totals = res.data.total
        this.listLoading = false;
      })
      /*getUserListPage(para).then((res) => {
        this.total = res.data.total;
        this.users = res.data.users;
        this.listLoading = false;
        //NProgress.done();
      });*/
    },
    //编辑(抽屉)
    saveSubmitTwo: function () {
      this.$confirm('确认提交吗？', '温馨提示', {})
          .then(() => {
            this.saveLoading = true;//开启忙的
            let para = Object.assign({}, this.saveFormTwo);//克隆
            /*let para = this.saveForm //直接赋值*/
            console.log(para)
            this.$http.put("/sdd",para).then(res=>{
              if(res.data.success){
                this.saveLoading = false;
                this.$message({
                  message: res.data.msg,
                  type: 'success'
                });
                //重置表单验证
                this.saveFormVisible = false;//关闭模态框
                this.getDetailsTwo();//刷新列表
              }else {
                this.$message({
                  message: res.data.msg,
                  type: 'warning'
                });
                this.saveLoading = false;
              }
            })
          });
    },
    //显示新增界面(抽屉)
    handleAddTwo: function () {
      this.saveTitle = "新增"
      this.saveFormTwo.id = null,
      this.saveFormTwo.name = null,
      this.saveFormVisible = true
    },
    //显示编辑界面(抽屉)
    handlesaveTwo: function (index, row) {
      this.saveTitle = "编辑"
      this.saveFormVisible = true;//是否显示
      this.saveFormTwo = Object.assign({}, row); //给表单赋值   要使用克隆
    },
    //获取抽屉信息
    getChouti(row){
      this.table = true;
      this.types_id= row.id
      this.saveFormTwo.types_id = row.id
      console.log(row)
      this.getDetailsTwo()
    },




    //获取用户列表
    getType() {
      let para = {
        currentPage: this.currentPage,
        pageSize: this.pageSize,
        //-------------------------关键字查询-------------------------------------
        keyword: this.filters.keyword
      };
      this.listLoading = true;
      //NProgress.start();
      this.$http.post("/sdt/queryList",para).then(res=>{
        /*console.log(res)*/
        this.types = res.data.date
        this.total = res.data.total
        this.listLoading = false;
      })
      /*gettypeListPage(para).then((res) => {
        this.total = res.data.total;
        this.types = res.data.types;
        this.listLoading = false;
        //NProgress.done();
      });*/
    },
    //删除
    handleDel: function (index, row) {
      this.$confirm('确认删除该记录吗?', '提示', {
        type: 'warning'
      }).then(() => {
        this.listLoading = true;
        this.$http.delete("/sdt/"+row.id).then(res=>{
          if(res.data.success){
            this.listLoading = false;
            //NProgress.done();
            this.$message({
              message: res.data.msg,
              type: 'success'
            });
            this.getType();
          }else{
            this.$message({
              message: res.data.msg,
              type: 'warning'
            });
          }
        })
      }).catch(() => {
      });
    },
    //显示编辑界面
    handlesave: function (index, row) {
      this.saveTitle = "编辑"
      this.saveFormVisible = true;//是否显示
      this.saveForm = Object.assign({}, row); //给表单赋值   要使用克隆
    },
    //显示新增界面
    handleAdd: function () {
      this.saveTitle = "新增"
      this.saveForm = {
        id: null,
        name: '',
        types_id: ''

      }
      this.saveFormVisible = true
    },
    //编辑
    saveSubmit: function () {
      this.$confirm('确认提交吗？', '温馨提示', {})
          .then(() => {
            this.saveLoading = true;//开启忙的
            /*let para = Object.assign({}, this.saveForm);//克隆*/
            let para = this.saveForm //直接赋值
            /*if(para.types_id){
              if(typeof para.types_id == "object"){
                para.types_id = para.types_id[para.types_id.length - 1];
              }
            }*/
            console.log(para)
            this.$http.put("/sdt",para).then(res=>{
              if(res.data.success){
                this.saveLoading = false;
                this.$message({
                  message: res.data.msg,
                  type: 'success'
                });
                //重置表单验证
                this.saveFormVisible = false;//关闭模态框
                this.getType();//刷新列表
              }else {
                this.$message({
                  message: res.data.msg,
                  type: 'warning'
                });
                this.saveLoading = false;
              }
            })

            // para.birth = (!para.birth || para.birth == '') ? '' : util.formatDate.format(new Date(para.birth), 'yyyy-MM-dd');
            // savedept(para).then((res) => {
            //   this.saveLoading = false;
            //   //NProgress.done();
            //   this.$message({
            //     message: '提交成功',
            //     type: 'success'
            //   });
            //   this.$refs['saveForm'].resetFields();
            //   this.saveFormVisible = false;
            //   this.getdepts();
            // });
          });
    },

    selsChange: function (sels) {
      this.sels = sels;
    },
    //批量删除
    batchRemove: function () {
      var ids = this.sels.map(item => item.id)
      this.$confirm('确认删除选中记录吗？', '提示', {
        type: 'warning'
      }).then(() => {
        //忙等开启
        this.listLoading = true;
        this.$http.patch("/sdt/del",ids).then(res=>{
          if(res.data.success){
            //如果执行成功关闭忙等
            this.listLoading = false;
            //NProgress.done();
            this.$message({
              message: res.data.msg,
              type: 'success'
            });
            this.getdepts();
          }else{
            this.$message({
              message: res.data.msg,
              type: 'warning'
            });
          }
        })
      }).catch(() => {

      });
    }
  },

  mounted() {
    this.getType();
    this.getdetail();
    this.getDetailsTwo();

  }
}

</script>

<style scoped>

</style>
